<template>
  <div>
    <div class="card" style="margin-bottom: 5px;">
      <el-input v-model="data.name" placeholder="请输入用户姓名查询" style="width: 240px"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px;">
      <el-table :data="data.tableData" stripe>
        <el-table-column prop="userName" label="用户姓名"></el-table-column>
        <el-table-column prop="type" label="类别">
          <template v-slot="scope">
            <el-tag type="success" v-if="scope.row.type === 'SINGER'">歌手</el-tag>
            <el-tag type="primary" v-if="scope.row.type === 'PLAYLIST'">歌单</el-tag>
            <el-tag type="info" v-if="scope.row.type === 'ALBUM'">专辑</el-tag>
            <el-tag type="warning" v-if="scope.row.type === 'SONG'">歌曲</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="名称">
          <template v-slot="scope">
            <span v-if="scope.row.type === 'SINGER'">{{ scope.row.singer.name }}</span>
            <span v-if="scope.row.type === 'PLAYLIST'">{{ scope.row.playlist.name }}</span>
            <span v-if="scope.row.type === 'ALBUM'">{{ scope.row.album.name }}</span>
            <span v-if="scope.row.type === 'SONG'">{{ scope.row.sing.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="time" label="收藏时间"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template v-slot="scope">
            <el-button type="danger" circle :icon="Delete" @click="del(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <div class="card" v-if="data.total">
      <el-pagination @current-change="load" background layout="total,prev, pager, next" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
    </div>
  </div>
</template>

<script setup>
import { reactive } from "vue"
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";
import {Delete, Edit} from "@element-plus/icons-vue";

const data = reactive({
  user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
  tableData: [],
  total: 0,
  pageNum: 1,  // 当前的页码
  pageSize: 5,  // 每页的个数
  name: null,
})

// 加载表格数据
const load = () => {
  request.get('/collect/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      userName: data.name
    }
  }).then(res => {
    if (res.code === '200') {
      data.tableData = res.data?.list || []
      data.total = res.data?.total
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const del = (id) => {
  ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗？', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/collect/delete/' + id).then(res => {
      if (res.code === '200') {
        ElMessage.success("删除成功")
        load()
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {
    console.error(err)
  })
}

const reset = () => {
  data.name = null
  load()
}

load()

</script>